<template>
  <div class="user-profile-container">
    <div class="profile-card">
      <div class="header-section">
        <el-button type="text" @click="router.back()" class="back-button">
          <el-icon><ArrowLeft /></el-icon> 返回
        </el-button>
        <h2>个人信息</h2>
      </div>
      <el-descriptions :column="1" border>
        <el-descriptions-item label="用户名">{{ userInfo.username }}</el-descriptions-item>
        <el-descriptions-item label="真实姓名">{{ userInfo.realName }}</el-descriptions-item>
        <el-descriptions-item label="邮箱">{{ userInfo.email }}</el-descriptions-item>
        <el-descriptions-item label="手机号">{{ userInfo.phone }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ userInfo.createTime }}</el-descriptions-item>
      </el-descriptions>

      <div class="password-section">
        <h3>修改密码</h3>
        <el-form :model="passwordForm" :rules="rules" ref="passwordFormRef" label-width="100px">
          <!-- <el-form-item label="原密码" prop="oldPassword">
            <el-input v-model="passwordForm.oldPassword" type="password" show-password></el-input>
          </el-form-item> -->
          <el-form-item label="新密码" prop="newPassword">
            <el-input v-model="passwordForm.newPassword" type="password" show-password></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input v-model="passwordForm.confirmPassword" type="password" show-password></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleUpdatePassword" :loading="loading">更新密码</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { ArrowLeft } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import request from '../utils/request'

const router = useRouter()

const userInfo = reactive({
  username: '',
  realName: '',
  email: '',
  phone: '',
  createTime: ''
})

const passwordFormRef = ref(null)
const loading = ref(false)

const passwordForm = reactive({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

const rules = {
  oldPassword: [{ required: true, message: '请输入原密码', trigger: 'blur' }],
  newPassword: [{ required: true, message: '请输入新密码', trigger: 'blur' }],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== passwordForm.newPassword) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

const getUserInfo = async () => {
  try {
    const res = await request({
      url: '/auth/info',
      method: 'get'
    })
    if (res.success) {
      Object.assign(userInfo, res.data)
    }
  } catch (error) {
    ElMessage.error('获取用户信息失败')
  }
}

const handleUpdatePassword = async () => {
  if (!passwordFormRef.value) return

  try {
    await passwordFormRef.value.validate()
    loading.value = true

    const res = await request({
      url: '/auth/password',
      method: 'put',
      data: {
        id: userInfo.id,
        password: passwordForm.newPassword
      }
    })

    if (res.success) {
      ElMessage.success('密码修改成功')
      passwordFormRef.value.resetFields()
    }
  } catch (error) {
    ElMessage.error(error.message || '密码修改失败')
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  getUserInfo()
})
</script>

<style scoped>
.user-profile-container {
  padding: 20px;
  display: flex;
  justify-content: center;
}

.profile-card {
  width: 100%;
  max-width: 800px;
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.header-section {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.back-button {
  margin-right: 16px;
  display: flex;
  align-items: center;
}

.back-button .el-icon {
  margin-right: 4px;
}

.password-section {
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid #eee;
}

h2 {
  margin-bottom: 20px;
  color: #303133;
}

h3 {
  margin-bottom: 20px;
  color: #303133;
}
</style>